/******************************************************************************

ADL SCORM 2004 4th Edition Sample Run-Time Environment

The ADL SCORM 2004 4th Ed. Sample Run-Time Environment is licensed under
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States.

The Advanced Distributed Learning Initiative allows you to:
  *  Share - to copy, distribute and transmit the work.
  *  Remix - to adapt the work. 

Under the following conditions:
  *  Attribution. You must attribute the work in the manner specified by the author or
     licensor (but not in any way that suggests that they endorse you or your use
     of the work).
  *  Noncommercial. You may not use this work for commercial purposes. 
  *  Share Alike. If you alter, transform, or build upon this work, you may distribute
     the resulting work only under the same or similar license to this one. 

For any reuse or distribution, you must make clear to others the license terms of this work. 

Any of the above conditions can be waived if you get permission from the ADL Initiative. 
Nothing in this license impairs or restricts the author's moral rights.

******************************************************************************/

package org.adl.validator.util;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * <strong>Filename: </strong>Messages.java<br><br>
 *
 * <strong>Description: </strong> <br>
 * The <code>Messages</code> creates a resource bundle that contains all of the
 * messages for the logging of the ADL CP Validator.    <br>
 *
 * @author ADL Technical Team<br><br>
 */
public class Messages_Improved
{
   /**
    * The fully qualified name of the resource bundle.  This bundle contains
    * all of the messages used during the Content Test Suite logging.
    */
   private static final String BUNDLE_NAME_IMPROVED = "org.adl.validator.util.zh_messages";
   //private static final String BUNDLE_NAME_IMPROVED = "org.adl.validator.util.messages_zh";

   /**
    * The formal reference to the <code>ResourceBundle</code>
    */
   private static final ResourceBundle RESOURCE_BUNDLE_IMPROVED = ResourceBundle
      .getBundle( BUNDLE_NAME_IMPROVED );
   
   /**
    * The fully qualified name of the secondary resource bundle to be used as needed
    */
   private static String SECONDARY_BUNDLE_NAME = "";
   
   /**
    * The reference to a secondary resource bundle to be used as needed
    */
   private static ResourceBundle SECONDARY_RESOURCE_BUNDLE = null;

   /**
    * Default constructor.  No explicitly defined functionality for this 
    * constructor.
    */
   private Messages_Improved()
   {
      // No explicitly defined functionality
   }

   /**
    * Returns the validator logging message associated with the given key. The method 
    * retrieves a parameterized message based on the input key and replaces the 
    * parameter with the input parameter. If the key is not found a default 
    * message based on the key will be returned.
    * 
    * @param iKey A unique identifier that identifies a message.
    * @return The message associated with the key.
    */
   public static String getString( String iKey )
   {
      try
      {
         return RESOURCE_BUNDLE_IMPROVED.getString( iKey );
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * Returns the validator logging message associated with the given key. The method 
    * retrieves a parameterized message based on the input key and replaces the 
    * parameter with the input parameter. If the key is not found a default 
    * message based on the key will be returned.
    * 
    * @param iKey A unique identifier that identifies the parameterized message.
    * 
    * @param iParam A parameter to be used to replace the parameterized value
    * in the message.
    * 
    * @return The message associated with the key.
    */
   public static String getString( String iKey, String iParam )
   {
      try
      {
         String message = RESOURCE_BUNDLE_IMPROVED.getString( iKey );
         iParam = mkRegxReady( iParam );      
         if ( iParam.length() >= 1000 )
         {
            iParam = "Original value removed due to size";
         }
         
         message = message.replaceAll( "param1",( iParam != null ) ? iParam : "" );

         return message;
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * Returns the validator logging message associated with the given key. The method 
    * retrieves a parameterized message based on the input key and replaces the 
    * parameter with the input parameter. If the key is not found a default 
    * message based on the key will be returned.
    * 
    * @param iKey A unique identifier that identifies the parameterized message.
    * 
    * @param iParam1 The first parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @param iParam2 The second parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @return The message associated with the key.
    */
   public static String getString( String iKey, String iParam1, String iParam2 )
   {
      try
      {
         String message = RESOURCE_BUNDLE_IMPROVED.getString( iKey );
         iParam1 = mkRegxReady( iParam1 );
         iParam2 = mkRegxReady( iParam2 );
         
         // Truncates large params for logging purposes 
         if ( iParam1.length() >= 1000 )
         {
            iParam1 = "[ Original value removed due to size ]";
         }
         message = message.replaceAll( "param1",( iParam1 != null ) 
            ? iParam1 : "" );
         
         // Truncates large params for logging purposes          
         if ( iParam2.length() >= 1000 )
         {
            iParam2 = "[ Original value removed due to size ]";
         }
         message = message.replaceAll( "param2",( iParam2 != null ) 
            ? iParam2 : "" );
         return message;
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * Returns the validator logging message associated with the given key. The method 
    * retrieves a parameterized message based on the input key and replaces the 
    * parameter with the input parameter. If the key is not found a default 
    * message based on the key will be returned.
    * 
    * @param iKey A unique identifier that identifies the parameterized message.
    * 
    * @param iParam1 The first parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @param iParam2 The second parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @param iParam3 The second parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @return The message associated with the key.
    */
   public static String getString( String iKey, String iParam1, String iParam2,
                                   String iParam3 )
   {
      try
      {
         String message = RESOURCE_BUNDLE_IMPROVED.getString( iKey );
         iParam1 = mkRegxReady( iParam1 );
         iParam2 = mkRegxReady( iParam2 );
         iParam3 = mkRegxReady( iParam3 );
         message = message.replaceAll( "param1",( iParam1 != null ) 
            ? iParam1 : ""  );
         message = message.replaceAll( "param2",( iParam2 != null ) 
            ? iParam2 : ""  );
         message = message.replaceAll( "param3",( iParam3 != null ) 
            ? iParam3 : ""  );
         return message;
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * Returns the validator logging message associated with the given key. The method 
    * retrieves a parameterized message based on the input key and replaces the 
    * parameter with the input parameter. If the key is not found a default 
    * message based on the key will be returned.
    * 
    * @param iKey A unique identifier that identifies the parameterized message.
    * 
    * @param iParam1 The first parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @param iParam2 An int parameter to be used to replace the parameterized 
    * value in the message.
    * 
    * @return The message associated with the key.
    */
   public static String getString( String iKey, String iParam1, int iParam2 )
   {
      try
      {
         String message = RESOURCE_BUNDLE_IMPROVED.getString( iKey );
         String replace = Integer.toString( iParam2 );
         iParam1 = mkRegxReady( iParam1 );      
         message = message.replaceAll( "param1",( iParam1 != null ) 
            ? iParam1 : "" );
         message = message.replaceAll( "param2", ( replace != null ) 
            ? replace : ""  );
         return message;
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * This method is used to format a string by searching for a specific string
    * and replacing the value. 
    * @param ioString The string that will be updated 
    * @param iOld The old string that will be used as the value to replace
    * @param iNew The new string that will replace the old string in the 
    *             returned string
    * @return Returns a new string with the replaced parameter. 
    */
   private static String replace( String ioString, String iOld, String iNew )
   {
      int startPos = 0;
      int indexPos = -1;
      String tempString;

      while ( ( indexPos = ioString.indexOf(iOld, startPos )) != -1 )
      {
         tempString = ioString.substring( 0, indexPos ) + iNew
            + ioString.substring( indexPos + iOld.length() );

         ioString = tempString;
         startPos = indexPos + iNew.length();
      }

      return ioString;
   }
   
   /**
    * This method looks for a given set of characters and replaces them with
    * a different set.
    * 
    * @param iParam The string that will be used during the replace procedure
    * 
    * @return Returns the newly replaced string.
    */
   private static String mkRegxReady(String iParam)
   {
      if ( iParam != null )
      {
         if ( iParam.indexOf("\\") != -1 )
         {
            iParam = replace(iParam, "\\", "\\\\");
         }
         if ( iParam.indexOf("$") != -1 )
         {
            iParam = replace(iParam, "$", "\\$");
         }
      }
      return iParam;
   }
   
   /**
    * Utility to convert potential null values to empty strings.
    * 
    * @param iString String that if null should be converted to empty string.
    * 
    * @return The string or empty string if string was null.
    */
   public static String unNull(String iString)
   {
      return (iString == null) ? "" : iString;
   }
   
   /**
    * Returns message associated with the given key.  The message is read
    * message from the given properties file
    * 
    * @param iPropFile - The fully qualified path to the properties file
    * @param iKey A unique identifier that identifies the parameterized message.
    * 
    * @return The message associated with the key.
    */
   public static String getMessage(String iPropFile, String iKey)
   {
      // Create a new bundle if one does not exist or the passed bundle is different
      if ( SECONDARY_RESOURCE_BUNDLE == null || !SECONDARY_BUNDLE_NAME.equals(iPropFile) )
      {
         SECONDARY_BUNDLE_NAME = iPropFile;
         SECONDARY_RESOURCE_BUNDLE = ResourceBundle.getBundle( SECONDARY_BUNDLE_NAME );
      }
      
      try
      {
         return SECONDARY_RESOURCE_BUNDLE.getString( iKey );
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
   
   /**
    * Returns message associated with the given key and parameter.  The message is read
    * message from the given properties file
    * 
    * @param iPropFile - The fully qualified path to the properties file
    * @param iKey A unique identifier that identifies the parameterized message.
    * @param iParam The first parameter to be used to replace the 
    * parameterized value in the message.
    * 
    * @return The message associated with the key.
    */
   public static String getMessage(String iPropFile, String iKey, String iParam)
   {
      // Create a new bundle if one does not exist or the passed bundle is different
      if ( SECONDARY_RESOURCE_BUNDLE == null || !SECONDARY_BUNDLE_NAME.equals(iPropFile) )
      {
         SECONDARY_BUNDLE_NAME = iPropFile;
         SECONDARY_RESOURCE_BUNDLE = ResourceBundle.getBundle( SECONDARY_BUNDLE_NAME );
      }
      
      try
      {
         String message = SECONDARY_RESOURCE_BUNDLE.getString( iKey );
         iParam = mkRegxReady( iParam );      
         if ( iParam.length() >= 1000 )
         {
            iParam = "Original value removed due to size";
         }
         
         message = message.replaceAll( "param1",( iParam != null ) ? iParam : "" );

         return message;
      }
      catch ( MissingResourceException e )
      {
         return '!' + iKey + '!';
      }
   }
}
